package com.graduation.fq.student.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.graduation.fq.student.Args.EventArgs;
import com.graduation.fq.student.Args.AlterLoginEventArgs;
import com.graduation.fq.student.Logic.LoginAndAlterPwdLogic;
import com.graduation.fq.student.R;
import com.graduation.fq.student.Utils.CommonUtils;
import com.graduation.fq.student.interfaces.EventListener;

public class LoginActivity extends Activity {

    private EditText idEdit;
    private EditText pwdEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取控件引用
        Button loginBt = (Button) findViewById(R.id.login_bt);
        TextView alterPwd = (TextView) findViewById(R.id.login_alter_pwd);
        idEdit = (EditText) findViewById(R.id.student_id_edit);
        pwdEdit = (EditText) findViewById(R.id.student_pwd_edit);

        //点击事件
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        alterPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterPwd();
            }
        });
    }

    /**
     * 修改密码
     */
    private void alterPwd() {
        startActivity(new Intent(LoginActivity.this, AlterPwdActivity.class));
    }

    /**
     * 登录
     */
    private void login() {
        final String stuId = idEdit.getText().toString();
        final String stuPwd = pwdEdit.getText().toString();
        if (CommonUtils.isNotNullOrEmpty(stuId, stuPwd)) {
            LoginAndAlterPwdLogic.getInstance().login(stuId, stuPwd, new EventListener() {
                @Override
                public void onEvent(EventArgs args) {
                    LoginAndAlterPwdLogic.getInstance().login(stuId, stuPwd, new EventListener() {
                        @Override
                        public void onEvent(EventArgs args) {
                            AlterLoginEventArgs result = (AlterLoginEventArgs) args;
                            if (result.isLogin()) {
                                CommonUtils.Toast(LoginActivity.this, "登录成功");
                                CommonUtils.saveUserId(LoginActivity.this, result.getId());
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            } else {
                                CommonUtils.Toast(LoginActivity.this, "登录失败, 请重试!");
                            }
                        }
                    });

                }
            });
        }
    }
}
